<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('organization.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col :span="4">
                  <el-form-item :label="i18n('organization.logo')" prop="logo">
                    <el-upload
                      class="avatar-uploader"
                      :action="$http.adornUrl('/file/picture')"
                      :show-file-list="false"
                      :headers="upload_headers"
                      :on-success="handleLogoSuccess"
                      :before-upload="beforePictureUpload">
                      <img v-if="dataForm.logo" :src="dataForm.logo" class="avatar-upload"/>
                      <el-icon v-else class="avatar-uploader-icon"><Plus/></el-icon>
                    </el-upload>
                    <el-link @click="handleLogoRemove" type="danger">
                      {{ i18n('common.delete') }}
                    </el-link>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-form-item :label="i18n('organization.title')" prop="title">
                    <el-input :placeholder="i18n('common.please_input') + i18n('organization.title')" v-model="dataForm.title"></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('organization.register_limit')" prop="title">
                    <el-input-number v-model="dataForm.register_limit" :min="1"/>
                  </el-form-item>

                  <el-form-item :label="i18n('organization.valid_time')" prop="title">
                    <el-date-picker v-model="dataForm.valid_time" type="date" :placeholder="i18n('common.please_input') + i18n('organization.valid_time')" clearable/>
                  </el-form-item>

                  <el-form-item :label="i18n('common.status')" prop="status">
                    <el-switch
                      v-model="dataForm.status"
                      active-value="1"
                      :active-text="i18n('common.enable')"
                      inactive-value="2"
                      :inactive-text="i18n('common.disable')">
                    </el-switch>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.detail_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col :span="4">
                  <el-form-item :label="i18n('organization.business_license')" prop="business_license">
                    <el-upload
                      class="avatar-uploader"
                      :action="$http.adornUrl('/file/picture')"
                      :show-file-list="false"
                      :headers="upload_headers"
                      :on-success="handleBusinessLicenseSuccess"
                      :before-upload="beforePictureUpload">
                      <img v-if="dataForm.business_license" :src="dataForm.business_license" class="avatar-upload"/>
                      <el-icon v-else class="avatar-uploader-icon"><Plus/></el-icon>
                    </el-upload>
                    <el-link @click="handleBusinessLicenseRemove" type="danger">
                      {{ i18n('common.delete') }}
                    </el-link>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-form-item :label="i18n('organization.manager_name')" prop="manager_name">
                    <el-input :placeholder="i18n('common.please_input') + i18n('organization.manager_name')" v-model="dataForm.manager_name"></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('organization.mobile')" prop="mobile">
                    <el-input :placeholder="i18n('common.please_input') + i18n('organization.mobile')" v-model="dataForm.mobile"></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('organization.address')" prop="address">
                    <el-input type="textarea" :placeholder="i18n('common.please_input') + i18n('organization.address')" v-model="dataForm.address"></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('organization.longitude')" prop="longitude">
                    <el-input :placeholder="i18n('common.please_input') + i18n('organization.longitude')" v-model="dataForm.longitude"></el-input>
                  </el-form-item>

                  <el-form-item :label="i18n('organization.latitude')" prop="latitude">
                    <el-input :placeholder="i18n('common.please_input') + i18n('organization.latitude')" v-model="dataForm.latitude"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('organization.contact_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('organization.contact_name')" prop="contact_name">
                    <el-input :placeholder="i18n('common.please_input') + i18n('organization.contact_name')" v-model="dataForm.contact_name"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('organization.contact_mobile')" prop="contact_mobile">
                    <el-input :placeholder="i18n('common.please_input') + i18n('organization.contact_mobile')" v-model="dataForm.contact_mobile"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('organization.contact_email')" prop="contact_email">
                    <el-input :placeholder="i18n('common.please_input') + i18n('organization.contact_email')" v-model="dataForm.contact_email"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('organization.contact_weixin')" prop="contact_weixin">
                    <el-input :placeholder="i18n('common.please_input') + i18n('organization.contact_weixin')" v-model="dataForm.contact_weixin"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('organization.contact_qq')" prop="contact_qq">
                    <el-input :placeholder="i18n('common.please_input') + i18n('organization.contact_qq')" v-model="dataForm.contact_qq"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:organization:handle')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="organization-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('organization')
  const dataForm = reactive({
    id: 0,
    logo: '',
    title: '',
    register_limit: 1,
    valid_time: '',
    business_license: '',
    manager_name: '',
    mobile: '',
    address: '',
    longitude: '',
    latitude: '',
    contact_name: '',
    contact_mobile: '',
    contact_email: '',
    contact_weixin: '',
    contact_qq: '',
    status: '1',
  })

  const dataRule = reactive({
    logo: [{
      required: true,
      message: i18n('organization.rules.logo.require'),
      trigger: 'blur',
    }],
    title: [{
      required: true,
      message: i18n('organization.rules.title.require'),
      trigger: 'blur',
    }],
    register_limit: [{
      required: true,
      message: i18n('organization.rules.register_limit.require'),
      trigger: 'blur',
    }],
    valid_time: [{
      required: true,
      message: i18n('organization.rules.valid_time.require'),
      trigger: 'blur',
    }],
    manager_name: [{
      required: true,
      message: i18n('organization.rules.manager_name.require'),
      trigger: 'blur',
    }],
    address: [{
      required: true,
      message: i18n('organization.rules.address.require'),
      trigger: 'blur',
    }],
    contact_name: [{
      required: true,
      message: i18n('organization.rules.contact_name.require'),
      trigger: 'blur',
    }],
    contact_mobile: [{
      required: true,
      message: i18n('organization.rules.contact_mobile.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    handleLogoSuccess,
    handleBusinessLicenseSuccess,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.logo = data.data.logo
            dataForm.title = data.data.title
            dataForm.register_limit = data.data.register_limit
            dataForm.valid_time = data.data.valid_time
            dataForm.total = data.data.total
            dataForm.status = data.data.status.value + ''
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/archive/data`),
              method: 'get',
              params: $http.adornParams({
                'organization_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                if(data.data) {
                  dataForm.business_license = data.data.business_license
                  dataForm.manager_name = data.data.manager_name
                  dataForm.mobile = data.data.mobile
                  dataForm.address = data.data.address
                  dataForm.longitude = data.data.longitude
                  dataForm.latitude = data.data.latitude
                }
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/contact/data`),
              method: 'get',
              params: $http.adornParams({
                'organization_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                if(data.data) {
                  dataForm.contact_name = data.data.contact_name
                  dataForm.contact_mobile = data.data.contact_mobile
                  dataForm.contact_email = data.data.contact_email
                  dataForm.contact_weixin = data.data.contact_weixin
                  dataForm.contact_qq = data.data.contact_qq
                }
              }
            })
          }
        })
      }
    })
  }

  const handleLogoRemove = () => {
    dataForm.logo = ''
  }

  const handleBusinessLicenseRemove = () => {
    dataForm.business_license = ''
  }

  onMounted(() => {
    init(formRef)
  })
</script>
